# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html

import json
import pymongo
from scrapy.conf import settings
from scrapy import log

from novel.items import BiQuGeCategoryItem
from novel.items import BiQuGeNovelItem
from novel.items import BiQuGeNovelChapterItem


class MongoPipelines(object):

    def __init__(self, mongourl, mongoport):
        '''
        mongodb 初始化
        '''
        self.mongourl = mongourl
        self.mongoport = mongoport

    @classmethod
    def from_crawler(cls, crawler):
        """
        mongodb 读取数据库配置
        """
        return cls(
            mongourl=crawler.settings.get("MONGODB_SERVER"),
            mongoport=crawler.settings.get("MONGODB_PORT")
        )

    def open_spider(self, spider):
        '''
        mongodb 链接链接
        '''
        self.client = pymongo.MongoClient(self.mongourl, self.mongoport)
        self.db = self.client[settings.get('MONGODB_BIQUGE_DB')]
        self.db.authenticate(settings.get('MONGODB_USER'),
                             settings.get('MONGODB_PWD'))

    def process_item(self, item, spider):
        '''
        保存数据
        '''
        if isinstance(item, BiQuGeCategoryItem):
            _find_Category = self.db.category.find_one(
                {
                    "source": item["source"],
                    "categoryName": item["categoryName"]
                })

            if _find_Category == None:
                log.msg(message='insert category:' +
                        json.dumps(dict(item), ensure_ascii=False))
                self.db.category.insert(dict(item))
            else:
                log.msg(message='update category:' +
                        json.dumps(dict(item), ensure_ascii=False))
                self.db.novel.update_one(
                    {"_id": _find_Category._id},
                    {"$set": {
                        "categoryUrl": item["categoryUrl"]
                    }})

        if isinstance(item, BiQuGeNovelItem):
            _find_novel = self.db.novel.find_one({
                "source": item["source"],
                "name": item["name"]
            })
            if _find_novel == None:
                log.msg(message='insert novel:' +
                        json.dumps(dict(item), ensure_ascii=False))
                self.db.novel.insert(dict(item))
            else:
                log.msg(message='update novel:' +
                        json.dumps(dict(item), ensure_ascii=False))
                self.db.novel.update_one(
                    {"_id": _find_novel._id},
                    {"$set": {
                        "updateDate": item["updateDate"],
                        "lastChapter": item["lastChapter"],
                        "lastChapterUrl": item["lastChapterUrl"]
                    }})

        if isinstance(item, BiQuGeNovelChapterItem):
            item['content'] = item['content'].replace(',\r\n', '')

            _find_chapter = self.db.chapter.find_one({
                "source": item["source"],
                "novelName": item["novelName"],
                "name": item["name"]
            })
            if _find_chapter == None:
                log.msg(message='insert chapter:' +
                        json.dumps(dict(item), ensure_ascii=False))
                self.db.chapter.insert(dict(item))
            else:
                log.msg(message='update chapter:' +
                        json.dumps(dict(item), ensure_ascii=False))
                self.db.chapter.update_one(
                    {"_id": _find_chapter._id},
                    {"$set": {
                        "novelName": item["novelName"],
                        "content": item["content"]
                    }})

        return item

    def close_spider(self, spider):
        # 关闭连接
        self.client.close()
